package com.zpb.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpb.framework.domain.ApiResult;
import com.zpb.framework.domain.dto.LinkAddDto;
import com.zpb.framework.domain.dto.LinkQueryDto;
import com.zpb.framework.domain.dto.LinkUpdateDto;
import com.zpb.framework.domain.entity.Link;
import com.zpb.framework.domain.enums.ResultCodeEnum;
import com.zpb.framework.domain.vo.LinkVo;
import com.zpb.framework.domain.vo.PageVo;
import com.zpb.framework.mapper.LinkMapper;
import com.zpb.framework.service.LinkService;
import com.zpb.framework.utils.BeanCopyUtil;
import com.zpb.framework.utils.DictCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Description:
 * @Author: zhaopengbo
 * @Date: 2025/9/10
 */
@Service
public class LinkServiceImpl implements LinkService {
    @Autowired
    private LinkMapper linkMapper;

    @Override
    public ApiResult<List<LinkVo>> getAllLink(Link link) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<Link>(link);
        queryWrapper.eq(Link::getStatus, DictCodeUtil.LINK_PASS);
        List<Link> linkList = linkMapper.selectList(queryWrapper);
        List<LinkVo> linkVoList = BeanCopyUtil.copyBeanList(linkList, LinkVo.class);
        return ApiResult.success(linkVoList);
    }

    @Override
    public ApiResult<PageVo> getLinkList(Integer pageNum, Integer pageSize, LinkQueryDto queryDto) {
        Link link = BeanCopyUtil.copyBean(queryDto, Link.class);
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Objects.nonNull(queryDto.getName()) && queryDto.getName().length() > 0, Link::getName, link.getName())
                .eq((Objects.nonNull(queryDto.getStatus())), Link::getStatus, link.getStatus())
                .orderByDesc(Link::getCreateTime);
        Page<Link> page = new Page<>(pageNum, pageSize);
        linkMapper.selectPage(page, wrapper);
        List<Link> links = page.getRecords();
        List<LinkVo> linkVoList = BeanCopyUtil.copyBeanList(links, LinkVo.class);
        PageVo pageVo = new PageVo(page.getTotal(), linkVoList);
        return ApiResult.success(pageVo);
    }

    @Override
    public ApiResult addLink(LinkAddDto linkAddDto) {
        Link link = BeanCopyUtil.copyBean(linkAddDto, Link.class);
        int result = linkMapper.insert(link);
        if (result>0){
            return ApiResult.success();
        }
        return ApiResult.failure(ResultCodeEnum.ADD_FAILURE);
    }

    @Override
    public ApiResult<LinkVo> getLinkById(Long id) {
        Link link = linkMapper.selectById(id);
        if (Objects.nonNull(link)){
            LinkVo linkVo = BeanCopyUtil.copyBean(link, LinkVo.class);
            return ApiResult.success(linkVo);
        }
        return ApiResult.failure(ResultCodeEnum.ADD_FAILURE);
    }

    @Override
    public ApiResult updateLink(LinkUpdateDto linkUpdateDto) {
        Link link = BeanCopyUtil.copyBean(linkUpdateDto, Link.class);
        int result = linkMapper.updateById(link);
        if (result>0){
            return ApiResult.success();
        }
        return ApiResult.failure(ResultCodeEnum.UPDATE_FAILURE);
    }

    @Override
    public ApiResult deleteLink(Long id) {
        Link link = linkMapper.selectById(id);
        if (Objects.isNull(link)){
            return ApiResult.failure(ResultCodeEnum.DEL_FAILURE);
        }
        int result = linkMapper.deleteById(id);
        if (result>0){
            return ApiResult.success();
        }
        return ApiResult.failure(ResultCodeEnum.DEL_FAILURE);
    }
}
